// pages/index/picture.js
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    picPaths: [],
    currentIndex: 0,//当前图片下标
    winHeight: 0,
    picHeight: 0,
    titleHeight: 0,
    Index: 0//Int型下标+1，即从1开始的下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '图片预览',
    });
     //  高度自适应
     var calc = 0;
     var picH = 0;
     const app = getApp();
     wx.getSystemInfo({
       success: function (res) {
         var clientHeight = res.windowHeight,
           clientWidth = res.windowWidth,
           rpxR = 750 / clientWidth,
           titleHeight = app.globalData.titleHeight;
         calc = clientHeight * rpxR * 0.8;
         picH = clientHeight * rpxR * 0.8;
         that.setData({
          winHeight: calc,
          picHeight: picH
         });
       }
     });
    //将传过来的数组显示出来
    let picPaths = options.picPaths.split(',');
    var index = options.index;
    this.setData({
      picPaths : picPaths,
      currentIndex : index,
      titleHeight: app.globalData.titleHeight,
      Index: parseInt(index) + 1
    });
  },
  bindBackTap : function(e){
    wx.navigateBack({
      delta: 1,
    });
  },
  bindDelTap : function(e){
    var index = e.currentTarget.dataset.index;
    var picPaths = this.data.picPaths;
    var currentIndex = this.data.currentIndex;
    var tempIndex = this.data.currentIndex;
    let self = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
      if  (res.confirm) {
          //如果是数组中末尾的图片，位置变成上一个
          if (currentIndex == picPaths.length - 1)
          {
            currentIndex = picPaths.length - 2;
          }
          //删除所选图片的数组所在位置
          picPaths.splice(tempIndex, 1);
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];
          //更新上一页图片数组
          prePage.setData({
            picPaths: picPaths,
          });
          //更新本页面图片数组
          self.setData({
            currentIndex: currentIndex,
            picPaths: picPaths,
            Index: parseInt(currentIndex) + 1
          });
          //如果删除完了直接返回
          if(picPaths.length == 0){
            wx.navigateBack({
              delta : 1
            });
          }
      } else if (res.cancel) {
          return false;
        }
        self.setData({
          picPaths: picPaths
        });
      }
    })
    
  },
  switchTab : function(e){
    this.setData({
      currentIndex: e.detail.current,
      Index: e.detail.current + 1
    });
  }
})